package jh.com.jhappbf.platenumber;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.fragment.app.Fragment;
import jh.com.jhappbf.R;
import jh.com.jhappbf.database.SQLiteDataBaseUtil;

/**
 * @author Administrator
 */
public class AreaFragment extends Fragment {

    private TextView mUsualTxt;
    private View mDivision1;
    private GridView mUsualGridView;

    private TextView mAllTxt;
    private View mDivision2;
    private GridView mAllGridView;
    private TextView mContentTxt;

    private List<String> mAllData;
    private List<String> mUsualData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.platenum_area, null);
        findView(v);
        initView();
        return v;
    }

    private void findView(View v) {
        mUsualTxt = v.findViewById(R.id.platenum_area_usual_tip);
        mDivision1 = v.findViewById(R.id.platenum_area_division1);
        mUsualGridView = v.findViewById(R.id.platenum_area_usual_gridView);
        mAllTxt = v.findViewById(R.id.platenum_area_all_tip);
        mDivision2 = v.findViewById(R.id.platenum_area_division2);
        mAllGridView = v.findViewById(R.id.platenum_area_all_gridView);
        mContentTxt = getActivity().findViewById(R.id.platenum_content_txt);
    }

    private void initView() {
        getUsual();
        mAllData = Arrays.asList(getResources().getStringArray(R.array.area));
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), R.layout.grdview_item, mAllData);
        mAllGridView.setAdapter(adapter);
        mAllGridView.setOnItemClickListener((parent, view, position, id) -> {
            String value = mAllData.get(position);
            setSelectItem(value);
        });
    }

    private void getUsual() {
        try {
            SQLiteDatabase db = SQLiteDataBaseUtil.getDatabase(getActivity());
            Cursor cursor = db.query("areas", new String[]{"areaName", "clickAccount"}, null, null, null, null, " clickAccount desc ", "0,6");
            mUsualData = new ArrayList<>();
            String name;
            while (cursor.moveToNext()) {
                name = cursor.getString(0);
                if (name.length() > 0) {
                    mUsualData.add(name);
                }
            }

            cursor.close();
            if (mUsualData.size() > 0) {
                ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), R.layout.grdview_item, mUsualData);
                mUsualGridView.setAdapter(adapter);
                mUsualGridView.setVisibility(View.VISIBLE);
                mDivision1.setVisibility(View.VISIBLE);
                mUsualTxt.setVisibility(View.VISIBLE);
                mUsualGridView.setOnItemClickListener((parent, view, position, id) -> {
                    String value = mUsualData.get(position);
                    setSelectItem(value);
                });
            } else {
                mUsualGridView.setVisibility(View.GONE);
                mDivision1.setVisibility(View.GONE);
                mUsualTxt.setVisibility(View.GONE);
            }

        } catch (Exception e) {
            Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 添加选择项
     *
     * @param area
     */
    private void addUsual(String area) {
        try {
            SQLiteDatabase db = SQLiteDataBaseUtil.getDatabase(getActivity());
            Cursor cursor = db.query("areas", new String[]{"areaName", "clickAccount"}, "areaName=?", new String[]{area}, null, null, null);
            if (cursor.moveToNext()) {
                ContentValues values = new ContentValues();
                values.put("clickAccount", cursor.getInt(1) + 1);
                db.update("areas", values, "areaName=?", new String[]{area});
            } else {
                ContentValues contentValues = new ContentValues();
                contentValues.put("areaName", area);
                contentValues.put("clickAccount", 1);
                db.insert("areas", null, contentValues);
            }

            cursor.close();
        } catch (Exception e) {
            Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 显示选择项
     *
     * @param value
     */
    private void setSelectItem(String value) {
        String mConString = mContentTxt.getText().toString();
        if (mConString.length() == 0) {
            mContentTxt.append(value);
        } else {
            for (int index = 0; index < mConString.length(); index++) {
                char c = mConString.charAt(index);
                String oldValue = c + "";
                if (!value.equals(oldValue) && mAllData.contains(oldValue)) {
                    mConString = mConString.replace(c, value.charAt(0));
                }
            }

            mContentTxt.setText(mConString);
        }
        addUsual(value);
    }
}
